
<script type="text/javascript">
	$(function(){
		refreshDisplayCurrency();
		$("#tabs").tabs({});
		setColumnSelect("cn_acc_id",":",2);	
		$("#tblCN").jqGrid({
			url:'index.php?r=keuangan/tu/studentPayment/invoiceList',
			//editUrl:'index.php?r=akademik/pendaftaran_kelas/kelas_tahun_ajaran/edit',
			postData:{},
			datatype: 'local',
			data:<?php echo $arrCn; ?>,
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			multiselect:false,
			colNames: ['id','Date','Account ID','Amount','Notes','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'cn_date', index: 'cn_date', width: 100,search:true,template:dateTemplate},
				{name: 'cn_acc_name', index: 'cn_acc_name', width: 200,search:true},				
				{name: 'ttl_cn', index: 'ttl_cn', width: 100,template:moneyTemplate},
				{name: 'notes', index: 'notes', width:300},
				{name: 'act', index: 'act', width: 50},				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblCN',
			viewrecords: true,
			caption: '',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblCN").getDataIDs();				 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i];
					row = $("#tblCN").jqGrid("getLocalRow",cl);  
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />";										
					$("#tblCN").setRowData(ids[i],{act:btnDelete})					 
				} 
			}                     		
		});
	});
	function save(){
		$("#frmData").submit();
	}
	function deleteRow(id){
		//var data = $("#tblCN").jqGrid("getRowData",id);
		if (confirm("Delete data credit note ?" )){			
			showLoader();						
			var editUrl = "index.php?r=keuangan/tu/studentInvoice/cnForm";			
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "delete",id:id},
				success: function(data) {
					if(data.actionStatus == 0){						
						//$("#tblCN").jqGrid("delRowData",id);
						location.reload();
					}
					if (data.actionMessage != ""){
						alert(data.actionMessage);
					} 
					if(data.actionError != "") {
						printConsole(data.actionError);
					}						
				},
				complete:function(){
					hideLoader();
				}
			});
		}
	}
	
	
	function cancel(){
		window.close();
	}
</script>

<div id="loader"> <img src='images/action/loader.gif' style="padding-top:20%"/> </div>

<div>	
	<div style="font-size:12px">
		<?php
			$labelWidth = "100px";
		?>		
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Invoice No :</label>
			<label> <?php echo $hInv->invoice_no;?></label>
		</div>
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Invoice Date :</label>
			<label> <?php echo $hInv->invoice_date;?></label>
		</div>
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Student :</label>
			<label> <?php echo $hInv->student->sid ." - ". $hInv->student->name ;?></label>
		</div>
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Transaksi :</label>
			<label> <?php echo $hInv->studentTransactionType->name; ?></label>
		</div>
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Total Invoice :</label>
			<label class="currency"> <?php echo $hInv->ttl_invoice; ?></label>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Disc :</label>
			<label class="currency"> <?php echo $hInv->ttl_disc; ?></label>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Paid :</label>
			<label class="currency"> <?php echo $hInv->ttl_paid; ?></label>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Credit Notes :</label>
			<label class="currency"> <?php echo $hInv->ttl_cn; ?></label>
		</div>
		<div>
			<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Notes :</label>
			<label> <?php echo $hInv->notes; ?></label>
		</div>
	</div>
	
	<div style="margin-top:10px">
		<div id="tabs">
			<ul>
				<li><a href="#add">Credit Note</a></li>
				<li><a href="#list">History</a></li>
			</ul>
			
			<div id="add">
				
				<div style="border-bottom:2px solid #000;padding-bottom:5px;margin-bottom:20px">
				<?php 
					$this->widget('application.components.widgets.ActionBar', 
						array("menuAlign" => "right", 
							'items' => array(
								array(
									'id' => "btnRefresh",
									'title' => "Simpan",
									'icon' => "images/action/Actions-document-save-all-icon.png",
									'callback'=>"save",
									'hidden'=>false,
								),					
								array(
									'id' => "btnCancel",
									'title' => "Batal",
									'icon' => "images/action/Actions-dialog-cancel-icon.png",
									'callback'=>"cancel"
								),
							)
						)
					);
				?>
				</div>
				
				<form id="frmData" method="POST" action="" >
				<div>
					<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right">Credit Note Date :</label>
					<?php
						$this->widget('application.components.widgets.DateTextField', array('name'=>'cn_date','value'=>date("d-M-Y")));
					?>
					<input type="text" id="id" name="id" value="<?php echo $hInv->id; ?>" />
					<input type="text" id="op" name="op" value="add" />
				</div>
				<div>
					<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right;vertical-align:top">Amount :</label>
					<input type="text" class="currency" id="ttl_cn" name="ttl_cn" value="<?php echo $hInv->ttl_invoice - ($hInv->ttl_disc + $hInv->ttl_paid + $hInv->ttl_cn)?>" />
				</div>
				<div>
					<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right;vertical-align:top">Account ID:</label>
					<select id="cn_acc_id" name="cn_acc_id">
						<?php
							foreach ($arrCnCOA as $coa){
								echo"<option value=".$coa->id .">".$coa->acc_code .":". $coa->name."</option>";
							} 
						?>
					</select>
				</div>
				<div>
					<label class="form" style="width:<?php echo $labelWidth;?>;text-align:right;vertical-align:top">Notes :</label>
					<textarea id="notes" name="notes" style="height:50px;width:300px"></textarea>
				</div>
				</form>
			</div>
			
			<div id="list">
				<table id="tblCN"></table>
				<div id="navTblCN"></div>
			</div>
		</div> 		
	</div>
</div>